package com.androidified.personalshopper.ui;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;

import com.androidified.personalshopper.BusProvider;
import com.androidified.personalshopper.CommerceProvider;
import com.androidified.personalshopper.Constants;
import com.androidified.personalshopper.R;
import com.androidified.personalshopper.bean.Commerce;
import com.androidified.personalshopper.bean.Promotion;
import com.cyrilmottier.polaris.Annotation;
import com.cyrilmottier.polaris.MapCalloutView;
import com.cyrilmottier.polaris.PolarisMapView;
import com.cyrilmottier.polaris.PolarisMapView.OnAnnotationSelectionChangedListener;
import com.google.android.maps.GeoPoint;
import com.squareup.otto.Subscribe;

public class MapActivity extends com.google.android.maps.MapActivity implements OnAnnotationSelectionChangedListener {

	private final List<Promotion> promotions = new ArrayList<Promotion>();
	
	private PolarisMapView mMapView;
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.map_activity);

        mMapView = new PolarisMapView(this, Constants.GOOGLE_MAPS_API_KEY);
        mMapView.setUserTrackingButtonEnabled(true);
        mMapView.setOnAnnotationSelectionChangedListener(this);
        
        final FrameLayout mapViewContainer = (FrameLayout) findViewById(R.id.map_view_container);
        mapViewContainer.addView(mMapView, new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        onNewPromotionsListEvent((ArrayList<Commerce>) CommerceProvider.get());
        
        getActionBar().setDisplayShowHomeEnabled(false);
	}
	
	@Override
    protected void onStart() {
        super.onStart();
        mMapView.onStart();
    }

    @Override
	protected void onResume() {
		super.onResume();
		BusProvider.get().register(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		BusProvider.get().unregister(this);
	}

	@Override
    protected void onStop() {
        super.onStop();
        mMapView.onStop();
    }
	
	@Subscribe public void onNewPromotionsListEvent(ArrayList<Commerce> commerces) {
		List<Annotation> annotations = new ArrayList<Annotation>();
		promotions.clear();
		
		for (Commerce commerce : commerces) {
			for (Promotion promo : commerce.getPromotions()) {
				annotations.add(new Annotation(new GeoPoint(
							(int) (commerce.getLocation()[0] * 1E6),
							(int) (commerce.getLocation()[1] * 1E6)),
						promo.getSubject(),
						promo.getDescription()));
				
				promotions.add(promo);
			}
		}
		
		mMapView.setAnnotations(annotations, R.drawable.pin);
	}
	

    @Override
    public void onAnnotationClicked(PolarisMapView mapView, MapCalloutView calloutView, int position, Annotation annotation) {
    	Intent intent = new Intent(this, DetailActivity.class);
    	intent.putExtra(DetailActivity.EXTRA_PROMO_ID, promotions.get(position).getId());
    	intent.putExtra(DetailActivity.EXTRA_COMMERCE_ID, promotions.get(position).getCommerce());
    	startActivity(intent);
    }
    
    @Override
    public void onAnnotationSelected(PolarisMapView mapView, MapCalloutView calloutView, int position, Annotation annotation) { }
    @Override
    public void onAnnotationDeselected(PolarisMapView mapView, MapCalloutView calloutView, int position, Annotation annotation) { }
    
    @Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == android.R.id.home) {
			finish();
			return true;
		}
		
		return super.onOptionsItemSelected(item);
	}
    
    
}
